package util;

import java.util.ArrayList;

import com.adventnet.snmp.beans.SnmpTable;
import com.adventnet.snmp.beans.SnmpTarget;

public class Lib {
	/**
	 * get sysServices (MIB .1.3.6.1.2.1.1.7.0)
	 * 
	 * @param dottedNetAddress
	 *            net address of target host
	 */
	public String getSysServices(String dottedNetAddress) {
		SnmpTarget target = new SnmpTarget();
		target.setTargetHost(dottedNetAddress);
		target.setObjectID(".1.3.6.1.2.1.1.7.0");
		return target.snmpGet();
	}
	
	/**
	 * get type of host
	 * 
	 * @param StringSysServices
	 *            sysServices in MIB
	 * @return 0 if host is a terminal, 1 otherwise(switch)
	 */
	public int parseSysServices(String StringSysServices) {
		if (StringSysServices.equals("76"))
			return 0;
		return 1;
	}
	
	/**
	 * get default gateway (MIB .1.3.6.1.2.1.4.21.1.7.0.0.0.0)
	 * 
	 * @param dottedNetAddress
	 *            net address of target host
	 */
	public String getDefaultGateway(String dottedNetAddress) {
		SnmpTarget target = new SnmpTarget();
		target.setTargetHost(dottedNetAddress);
		target.setObjectID(".1.3.6.1.2.1.4.21.1.7.0.0.0.0");
		return target.snmpGet();
	}

	/**
	 * get name (MIB .1.3.6.1.2.1.1.5.0) , i.e. displayString defined in RFC1213
	 * 
	 * @param dottedNetAddress
	 *            IP address of target host
	 */
	public String getName(String dottedNetAddress) {
		SnmpTarget target = new SnmpTarget();
		target.setTargetHost(dottedNetAddress);
		target.setObjectID(".1.3.6.1.2.1.1.5.0");
		return target.snmpGet();
	}

	/**
	 * get ipRouteTable(ARP Table) (MIB IP-21)
	 * 
	 * @param dottedNetAddress
	 *            net address of target host
	 * @throws Exception
	 */
	public SnmpTable getIpRouteTable(String dottedNetAddress) throws Exception {
		SnmpTable table = new SnmpTable();
		table.setTargetHost(dottedNetAddress);

		// To load MIBs from compiled file
		table.setLoadFromCompiledMibs(true);
		table.loadMibs(LIB_MIB + _RFC1213);
		table.setTableOIDWoStart("ipRouteTable");
		table.run();
		return table;
	}
	
	/**
	 * get ipNetToMediaTable (MIB IP-22)
	 * 
	 * @param dottedNetAddress
	 *            net address of target host
	 * @throws Exception
	 */
	public SnmpTable getIpNetToMediaTable(String dottedNetAddress) throws Exception {
		SnmpTable table = new SnmpTable();
		table.setTargetHost(dottedNetAddress);

		// To load MIBs from compiled file
		table.setLoadFromCompiledMibs(true);
		table.loadMibs(LIB_MIB + _RFC1213);
		table.setTableOIDWoStart("ipNetToMediaTable");
		table.run();
		return table;
	}

	/**
	 * convert from SnmpTable to table with element of String
	 * 
	 * @param table
	 *            source SnmpTable
	 * @param display
	 *            whether print
	 */
	public ArrayList<ArrayList<Object>> displayTable(SnmpTable table, boolean display) {
		ArrayList<ArrayList<Object>> res = new ArrayList<ArrayList<Object>>();
		int rowNum = table.getRowCount(), colNum = table.getColumnCount();

		/* convert */
		for (int i = 0; i < rowNum; i++) {
			ArrayList<Object> row = new ArrayList<Object>();
			for (int j = 0; j < colNum; j++)
				row.add(table.getValueAt(i, j));
			res.add(row);
		}

		/* print on console */
		if (display) {
			StringBuffer sb = new StringBuffer();

			// print name of columns
			for (int i = 0; i < colNum; i++) {
				sb.append(table.getColumnName(i) + " \t");
			}
			System.out.println(sb.toString());

			// print the rows we're getting
			sb = new StringBuffer();
			for (int i = 0; i < rowNum; i++) {
				for (int j = 0; j < colNum; j++)
					sb.append(table.getValueAt(i, j) + " \t");
				sb.append("\n");
			}
			System.out.println(sb.toString());
		}
		
		return res;
	}
	
	public String getIpRouteDest(ArrayList<ArrayList<Object>> ipRouteTable, int rowIndex) {
		return ipRouteTable.get(rowIndex).get(index_ipRouteDest).toString();
	}
	
	public String getIpRouteNextHop(ArrayList<ArrayList<Object>> ipRouteTable, int rowIndex) {
		return ipRouteTable.get(rowIndex).get(index_ipRouteNextHop).toString();
	}
	
	public String getIpRouteType(ArrayList<ArrayList<Object>> ipRouteTable, int rowIndex) {
		return ipRouteTable.get(rowIndex).get(index_ipRouteType).toString();
	}
	
	public String getIpRouteMask(ArrayList<ArrayList<Object>> ipRouteTable, int rowIndex) {
		return ipRouteTable.get(rowIndex).get(index_ipRouteMask).toString();
	}
	
	public String getIpNetToMediaPhysAddress(ArrayList<ArrayList<Object>> ArpTable, int rowIndex) {
		return ArpTable.get(rowIndex).get(index_ipNetToMediaPhysAddress).toString();
	}
	
	public String getIpNetToMediaNetAddress(ArrayList<ArrayList<Object>> ArpTable, int rowIndex) {
		return ArpTable.get(rowIndex).get(index_ipNetToMediaNetAddress).toString();
	}
	
	public int parseIpRouteType(String StringType) {
		if (StringType.startsWith("other"))
			return 1;
		if (StringType.startsWith("invalid"))
			return 2;
		if (StringType.startsWith("direct"))
			return 3;
		if (StringType.startsWith("indirect"))
			return 4;
		return -1; // if error
	}

	private static String LIB_MIB = "D:/SNMPAPI/mibs/";
	private static String _RFC1213 = "RFC1213-MIB";
	private static final int index_ipRouteDest = 0;
	private static final int index_ipRouteNextHop = 6;
	private static final int index_ipRouteType = 7;
	private static final int index_ipRouteMask = 10;
	private static final int index_ipNetToMediaPhysAddress = 1;
	private static final int index_ipNetToMediaNetAddress = 2;
}
